package actions;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import db.dao.Classes;
import db.dao.ClassesDAO;
import db.dao.Dept;
import db.dao.DeptDAO;
import db.dao.Major;
import db.dao.MajorDAO;
/*取消学院/专业/班级*/
public class DeleteCMDAction extends ActionSupport{
	private String id;	//编号
	private String deleteOption;	//选项
	
	public DeleteCMDAction() {
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getDeleteOption() {
		return deleteOption;
	}

	public void setDeleteOption(String deleteOption) {
		this.deleteOption = deleteOption;
	}

	public String execute() throws IOException {
		//获取页面信息，用于弹窗提示和清除页面缓存
		HttpServletResponse response = ServletActionContext.getResponse();
		//用于获取结果
		String result="";
		//判断用户选择的选项并根据输入的编号查找相应的班级、专业、学院
		if (deleteOption.equals("class")) {
			ClassesDAO cDao = new ClassesDAO();
			Classes classes = cDao.findById(id);
			result = cDao.delete(classes);
		}else if (deleteOption.equals("major")) {
			MajorDAO mDao = new MajorDAO();
			Major major = mDao.findById(id);
			result = mDao.delete(major);
		}else if (deleteOption.equals("dept")) {
			DeptDAO dDao = new DeptDAO();
			Dept dept = dDao.findById(id);
			result = dDao.delete(dept);
		}
		if (result.equals("success")) {
			//成功弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('取消成功!')</script>");
            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}else {
			//失败弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('取消失败!')</script>");
            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}
	}
}
